<template>
  <div id="message-reply">
    <div class="items">
      <div class="item">
        <div class="header clearfix">
          <div class="left-side pull-left">
            <span class="number">{{detail.id}}</span>
            <span class="label">{{detail.label_top_name}}</span>
            <span class="label">{{detail.label_second_name}}</span>
          </div>
          <div class="right-side pull-right">{{detail.date}}</div>
        </div>
        <div class="content">{{detail.question}}</div>
        <div class="reply" v-show="detail.answer">
          <img src="../../assets/message.png" alt="">
          {{detail.answer}}
        </div>
      </div>
    </div>
    <el-form ref="form" :model="formInline" :rules="rules" label-width="80px" size="mini">
      <el-form-item label="" prop="answer" style="position: relative;margin-top: 20px; padding-left: 10px;">
        <img style="position: absolute;top:0;left: -40px;height: 25px;width: 25px;" class="pull-left" src="../../assets/message.png" alt="">
        <el-input
          v-model="formInline.answer"
          type="textarea"
          placeholder="请输入"
          :rows="10"
          resize="none"
        />
      </el-form-item>
      <el-form-item size="large">
        <el-button class="pull-right" type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { firstLabel, secondLabel, getMessage, upateById } from '@/api/message'
import { parseTime } from '@/utils'
export default {
  data() {
    return {
      detail: '',
      formInline: {
        answer: '',
        question: '',
        label_top: '',
        label_second: ''
      },
      rules: {
        answer: [
          { required: true, message: ' ', trigger: 'blur' }
        ]
      },
      label_top: [],
      label_second: []
    }
  },
  watch: {
    'formInline.label_top': {
      handler() {
        this.getSecondLabel()
      },
      immediate: true
    }
  },
  mounted() {
    this.getdetail()
    this.getFirstLabel()
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const params = Object.assign(this.formInline, {
            id: this.$route.params.id,
            label_top_name: this.formInline.label_top
              ? this.label_top.filter(({ value, label }) => Number(value) === Number(this.formInline.label_top))[0].label
              : '',
            label_second_name: this.formInline.label_second
              ? this.label_second.filter(({ id, label }) => Number(id) === Number(this.formInline.label_second))[0].label
              : ''
          })
          upateById(params).then(res => {
            const code = Number(res.code)
            if (code === 200) {
              this.$router.push('/message/list')
            }
          })
        }
      })
    },
    async getdetail() {
      const res = await getMessage(this.$route.params.id)
      const code = Number(res.code)
      if (code === 200) {
        this.detail = res.data
        this.detail.date = parseTime(res.data.create_time)
        this.formInline.question = res.data.question
        this.formInline.label_top = res.data.label_top
        this.formInline.label_second = res.data.label_second
      }
    },
    async getFirstLabel() {
      const res = await firstLabel({ value: 9 })
      const code = Number(res.code)
      if (code === 200) {
        this.label_top = res.data.map(({ label, value }) => ({ label, value: Number(value) }))
      }
    },
    async getSecondLabel() {
      const res = await secondLabel({ value: this.formInline.label_top })
      const code = Number(res.code)
      if (code === 200) {
        this.label_second = res.data
      }
    }
  }
}
</script>
<style lang="scss" scoped>
#message-reply{
  padding-right: 50px;
  .header{
    .el-form-item{
      min-width: 150px;
      margin-bottom: 0;
    }
    .el-form-item:nth-child(1){
      margin-left: 40px;
    }
  }
  .date{
    margin: 0 0 40px 40px;
    font-size: 14px;
    color: #999;
  }
  .items{
    .header{
      padding-top: 35px;
      padding-left: 22px;
      .number{
        font-size: 18px;
        font-family: PingFangSC, PingFangSC-Semibold;
        font-weight: 600;
        text-align: left;
        color: rgba(0,0,0,0.85);
        line-height: 25px;
      }
      .label{
        margin-left: 16px;
        font-size: 14px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: left;
        color: #fc7a01;
        line-height: 14px;
      }
      .right-side{
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: left;
        color: rgba(0,0,0,0.45);
        line-height: 22px;
      }
    }
    .content{
      margin-top: 5px;
      padding-left: 50px;
      font-size: 14px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: rgba(0,0,0,0.85);
      line-height: 25px;
      height: 75px;
      overflow:hidden;
      text-overflow:ellipsis;
      display:-webkit-box;
      -webkit-box-orient:vertical;
      -webkit-line-clamp:3;
    }
    .reply{
      position: relative;
      background: #f8fcff;
      border-radius: 2px;
      padding: 10px;
      padding-left: 50px;
      margin-left: 50px;
      height: 75px;
      color: rgba(0,0,0,0.65);
      overflow:hidden;
      text-overflow:ellipsis;
      display:-webkit-box;
      -webkit-box-orient:vertical;
      -webkit-line-clamp:3;
      img{
        position: absolute;
        left: 10px;
        top: 5px;
        width: 25px;
        height: 25px;
      }
    }
  }
}
</style>
